<?php
/*
 * Created on Mar 27, 2009
 * Author: ANH
 * fileName: AjaxController.php
 * redraw imamge
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class Common_AjaxController extends Clu_Controller_Action_Hvn
 {
 	public function init()
 	{
 		//require_once (APP_PATH.'/modules/place/Model/Place.php');
 		//require_once (APP_PATH.'/modules/place/Model/District.php');
 		//require_once (APP_PATH.'/modules/place/Model/Village.php');
 		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/category/Model/Category.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		$this->m = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		$this->mDistrict = new place_Model_District();
		$this->mVillage = new place_Model_Village();
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('place');
		$this->mEvent = new event_Model_Service();
		parent::init();
 	}
	public function redrawAction()
	{
			$height = $this->_request->getParam('height', 40);
			$width = $this->_request->getParam('width', 85);
			$captcha = new Zend_Captcha_Image(array(
					 'captcha' => 'Image',
					 'wordLen' => 4,
					 'timeout' => 300, 
					 'width' => $width,
					 'height' => $height,
					 'DotNoiseLevel' => 1,  
					 'LineNoiseLevel' => 1,
					 'imgUrl' => APP_PATH.'/public/captcha/',
					 'imgDir'  =>APP_PATH.'/public/captcha', 
			         'font' => APP_PATH.'/public/captcha/font/tahoma.ttf',
			         'size' => 8,  
			));  
			  
			$captchaId = $captcha->generate();
			$ret['url'] = '/public/captcha/' . $captchaId . '.png';
			$ret['captchaId'] = $captchaId;
			$this->_helper->json->sendJson($ret);
	}
	
//	public function bulkPlaceAction()
//    {        
//        if($this->_request->isXmlHttpRequest())
//        {
//	        $ids = $this->_request->getParam('event_ids');
//	        $ret['ids'] = $ids;
//	        if (count($ids) > 0)
//	        {
//	            $action = $this->_request->getParam('bulkaction');
//		        if ($action === 'delete')
//		        {
//		            if ($this->m->deleteByPrimary($ids))
//		            	$ret['success'] = true;
//		        }
//		        else if($action === 'status')
//		        {
//	                $ret['status'] = $this->_request->getParam('changestatus');
//	                if ($this->m->bulkUpdateStatus($ids, $ret))
//	                    $ret['success'] = true;       
//		        }
//	        }
//	        else
//	            $ret['success'] = false;
//	        $ret['bulkaction'] = $action;
//	        $this->_helper->json->sendJson($ret);
//        }
//    }
	
	public function bulkModuleAction()
    {        
        if($this->_request->isXmlHttpRequest())
        {
	    	$mplaces = $this->_request->getParams();
        	$ret = array();
        	//check permission
		    if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
			if(!empty($mplaces['places']))
			{
				$modules = $mplaces['places'];
				$models = $this->m;
			}
			elseif(!empty($mplaces['event_ids']))
			{
				$modules = $mplaces['event_ids'];
				$models = $this->mEvent;
			}
	        if (count($modules) > 0)
	        {
	            $action = $this->_request->getParam('bulkaction');
		        if ($action === 'delete')
		        {
		            if (!empty($mplaces['places']))
		            {
		            	if($models->deleteBulk($modules) === true)
			            	$ret['success'] = true;
			            else
			            	$ret['success'] = false;
		            }
		            else if($models->deleteByPrimary($modules) === true)
		            		$ret['success'] = true;
		            	 else 
		            	 	$ret['success'] = false;
					
		        }
		        else if($action === 'status')
		        {
	                $ret['status'] = $this->_request->getParam('changestatus');
	                if($models->bulkUpdateStatus($modules, $ret))
	                {
	                	$ret['success'] = true;
	                }
		        }
	        }
	        else
	            $ret['success'] = false;
	        $ret['ids'] = $modules;
	        $ret['bulkaction'] = $action;
	        $this->_helper->json->sendJson($ret);
        }
    }
    
    public function autocompleteAction()
	{
		include APP_PATH.'/public/library/autocomplete/unaccent.php';
		
		$connect = mysql_connect('localhost','root','123')or die("Error connection:".mysql_error());
		mysql_select_db('anh', $connect) or die ("Error:".mysql_error());
		
		$sql = 'SELECT id, cityname, uncityname FROM test';
		$result = mysql_query($sql);
		// check the parameter
		if(isset($_GET['part']) and $_GET['part'] != '')
		{	
			$part = VN_unaccent(strtolower($_GET['part']));
	        $parts = explode (' ',$part);
			$results = array();
			$names = array();
			$ids = array();
			while($rows=mysql_fetch_array($result))
			{
				$items = array($rows['uncityname'] => $rows['cityname'] );
		 		foreach ($items as $key => $value) 
		 		{
	                $matched = true;
	                $lower_key = strtolower($key);
	                foreach ($parts as $p)
	                {
	                    if (strpos($lower_key, $p) === false) 
	                    {
	                       $matched = false;
	                       break;
	                    }
	                }
	               if ($matched)
		   			{
		   				$id = array($rows['id']);
						$results[] = array('name' => $value, 'id' => $id);
			  		}
	 			}
			}
			
			$this->_helper->json->sendJson($results);
		}
	}
 }
?>
